<?php

	require_once(INCLUDE_PATH.'db.inc.php');
	class Vote extends DBSQL {
		
		public $_name = "EM_VOTE_INFO";   //定义调查表名称变量
		public $_item = 'EE_ITEM_INFO';   //定义调查选项表名称变量
		public $_user = 'EE_VOTE_USER';   //定义用户信息表名称变量
		public $_pagesize = 10;           //定义每页提取记录数
		
		public $_type = array("1"=>"单选", "2"=>"多选");
		public $_display = array("0"=>"禁用", "1"=>"启用");
		
		public function __construct(){
			parent::__construct();
		}
		
		/**
		  * 功能：提取调查列表
		  * 返回：数组
		 */
		public function getVoteList(){
			
			$sql = "SELECT * FROM ".$this->_name;
			return $this->select($sql);	
			
		}
		
		/**
		  * 功能：提取指定表的指定ID的记录
		  * 参数：$id 表ID， $name表名称
		  * 返回：数组
		 */
		 public function getInfo($id, $name){
			 
			 $sql = "SELECT * FROM ".$name." where F_ID = $id";
			 
			 $r = $this->select($sql);
			 return $r[0];
		 }
		 /**
		   * 功能：向指定表中插入数据
		   * 参数：$name 表名称，$data 数组(格式：$data['字段名'] = 值)
		   * 返回：插入记录ID
		  */
		 public function insertData($name, $data){
			 
			 $field = implode(',',array_keys($data));                  //定义SQL语句的字段部分
			 $value = "";
			 $i = count($data) - 1;
			 foreach($data as $key => $val){							//组合SQL语句的值部分
				$value .= "'" . $val . "'";
				
				if($i > 0){
						                            //判断是否到数组的最后一个值
					$value .= ",";	
					$i --;
				}
							
			 }
			 
			 $sql = "INSERT INTO " . $name . "(" . $field . ") VALUES(" . $value . ")";
			
			 
		     return $this->insert($sql);

			 
		 }
		 
		/* 功能：更新指定表指定ID的调查表记录
		 * 参数：$name 表名称，$id 表ID，$data 数组(格式：$data['字段名'] = 值)
		 * 返回：TRUE OR FALSE
		 */
		public function updateData($name,$id,$data){	 
			$col = array();
			foreach ($data as $key => $value)
			{
				$col[] = $key . "='" . $value . "'";
			}
			$sql = "UPDATE " . $name . " SET " . implode(',',$col) . " WHERE F_ID = $id";
			return $this->update($sql);
		}
		
		/**
	* 功能：删除指定ID的调查表记录及相关表记录
	* 参数：$id 调查表ID
	* 返回：TRUE OR FALSE
	*/
	public function delData($id){
		$this->begintransaction();
		try{
			$sql = "DELETE FROM " . $this->_item . " WHERE F_ID_VOTE_INFO = " . $id;
			$this->delete($sql);								//删除调查选项里面的相关数据
			$sql = "DELETE FROM " . $this->_user . " WHERE F_ID_VOTE_INFO = " . $id;
			$this->delete($sql);								//删除用户统计表里面的相关数据
			$sql = "DELETE FROM " . $this->_name . " WHERE F_ID = " . $id;
			$this->delete($sql);
		}catch(Exception $e){
			$this->rollback();
			return false;
		}
		$this->commit();
		return true;
	}
	/**
	 * 功能：提取指定调查ID的选项
	 * 参数：$vote_id 调查ID
	 * 返回：数组
	 */
	public function getItemList($vote_id)
	{
		$sql = "SELECT * FROM " . $this->_item . " WHERE F_ID_VOTE_INFO = $vote_id";
		return $this->select($sql);
	}
	/**
	 * 功能：删除指定ID的选项表记录
	 * 参数：$id 表ID
	 * 返回：TRUE OR FALSE
	 */
	public function delItemData($id)
	{
		$sql = "DELETE FROM " . $this->_item . " WHERE F_ID = $id";
		return $this->delete($sql);
	$sql .= " ORDER BY F_ITEM_ORDER";}
	/**
	 * 功能：提取指定调查ID的用户统计信息
	 * 参数：$vote_id 调查ID，$page 当前页码
	 * 返回：数组
	 */
	public function getUserList($vote_id,$page=1)
	{
		$start = ($page - 1) * $this->_pagesize;
		$sql = "SELECT * FROM " . $this->_user . " WHERE F_ID_VOTE_INFO = $vote_id";
		$sql .= " LIMIT $start,$this->_pagesize";
		return $this->select($sql);
	}
	/**
	 * 功能：提取指定调查ID用户统计记录的条数
	 * 参数：$vote_id 调查ID
	 * 返回：记录条数
	 */
	public function getUserCount($vote_id)
	{
		$sql = "SELECT COUNT(F_ID) FROM " . $this->_user . " WHERE F_ID_VOTE_INFO = $vote_id";
		$r = $this->select($sql);
		return $r[0][0];
	}
	/**
	 * 功能：删除指定ID的用户统计记录
	 * 参数：$id 用户统计表ID
	 * 返回：TRUE OR FALSE
	 */
	public function delUserData($id)
	{
		$sql = "DELETE FROM " . $this->_user . " WHERE F_ID = $id";
		return $this->delete($sql);
	}
}
?>
